package middle;

public class Solution_162 {

    public int findPeakElement(int[] nums) {
//        return find(nums, -1, nums.length);
        System.out.println();
        for (int i = 0; i < nums.length; i++) {
            long left_val = i == 0 ? Long.MIN_VALUE : nums[i - 1];
            long right_val = i == (nums.length - 1) ? Long.MIN_VALUE : nums[i + 1];
            if (left_val < nums[i] && right_val < nums[i]) {
                return i;
            }
        }
        return -1;
    }

    private int find(int[] nums, int begin, int end) {
        int middle = (begin + end) >> 1;
        middle = middle < 0 ? 0 : middle >= nums.length ? nums.length - 1 : middle;
        long left_val = (middle - 1) < 0 ? Long.MIN_VALUE : nums[middle - 1];
        long right_val = (middle + 1) >= nums.length ? Long.MIN_VALUE : nums[middle + 1];

        if (left_val < nums[middle] && right_val < nums[middle]) {
            return middle;
        }

        if (begin >= end) {
            return -1;
        }

        int l_index = find(nums, begin, middle - 1);
        int r_index = find(nums, middle + 1, end);
        return Math.max(l_index, r_index);
    }

    public static void main(String[] args) {
        Solution_162 model = new Solution_162();
        System.out.println(model.findPeakElement(new int[]{5, 4}));
        System.out.println(model.findPeakElement(new int[]{1, 4}));
        System.out.println(model.findPeakElement(new int[]{1}));
        System.out.println(model.findPeakElement(new int[]{1, 2, 3, 1}));
        System.out.println(model.findPeakElement(new int[]{1, 2, 1, 3, 5, 6, 4}));
    }
}
